package com.cpos.bkge.controller;

import com.cpos.bkge.base.ResultInfo;
import com.cpos.bkge.entity.CposAccountFans;
import com.cpos.bkge.mapper.CposAccountFansMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * cpos_account_fans
 * @author Cpos_lwl 2020-08-11
 */
@RestController
@RequestMapping("/bkge/accountFans")
public class CposAccountFansController {

    @Autowired
    private CposAccountFansMapper cposAccountFansMapper;

    /**
     * 新增或编辑
     */
    @PostMapping("/save")
    public ResultInfo save(CposAccountFans cposAccountFans){

        ResultInfo resultInfo= new ResultInfo();

        String uuid = cposAccountFans.getUuid();
        if(StringUtils.isEmpty(uuid)){
            cposAccountFansMapper.insert(cposAccountFans);
        }else{
            cposAccountFansMapper.updateById(cposAccountFans);
        }
        resultInfo.setData(cposAccountFans);
        return resultInfo;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public ResultInfo delete(String uuid){
        ResultInfo resultInfo= new ResultInfo();

        CposAccountFans cposAccountFans = cposAccountFansMapper.selectOne(new QueryWrapper<CposAccountFans>().eq("uuid",uuid));
        if(cposAccountFans!=null){
            resultInfo.setData(cposAccountFans);
            return resultInfo;
        }else{
            resultInfo.setData("没有找到该对象");
            return resultInfo;
        }
    }

    /**
     * 查询
     */
    @PostMapping("/find")
    public ResultInfo find(String uuid){

        ResultInfo resultInfo= new ResultInfo();

        CposAccountFans cposAccountFans = cposAccountFansMapper.selectOne(new QueryWrapper<CposAccountFans>().eq("uuid",uuid));
        if(cposAccountFans!=null){
            resultInfo.setData(cposAccountFans);
            return resultInfo;
        }else{
            resultInfo.setData("没有找到该对象");
            return resultInfo;
        }
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(CposAccountFans cposAccountFans,
                       @RequestParam(required = false, defaultValue = "1") int pageNumber,
                       @RequestParam(required = false, defaultValue = "10") int pageSize) {

        ResultInfo resultInfo= new ResultInfo();

        //分页构造器
        Page<CposAccountFans> page = new Page<CposAccountFans>(pageNumber,pageSize);
        //条件构造器
        QueryWrapper<CposAccountFans> queryWrapperw = new QueryWrapper<CposAccountFans>(cposAccountFans);
        //执行分页
        IPage<CposAccountFans> pageList =  cposAccountFansMapper.selectPage(page,queryWrapperw);

        resultInfo.setData(pageList);
        //返回结果
        return resultInfo;
    }

}
